/*
 *  $TEAM_NAME Space Trader
 */
package views;

import models.StarSystem;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.File;

/**
 * Star System Panel
 * @author twb
 * @version 1.0
 */
public class StarSystemPanel extends JPanel {
	/**
	 * field
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * field
	 */
	private final StarSystem system;

	/**
	 * field
	 */
	private boolean current;

	/**
	 * field
	 */
	private boolean inRange;

	/**
	 * field
	 */
	private BufferedImage blueStar = null;
    
	/**
	 * field
	 */
	private BufferedImage redStar = null;
    
	/**
	 * field
	 */
	private BufferedImage greenStar = null;

	/**
	 * Creates a panel for the system
	 * @param system
	 */
    public StarSystemPanel(StarSystem system) {
        try {
            blueStar = ImageIO.read(new File("resources/BlueStar.png"));
            redStar = ImageIO.read(new File("resources/RedStar.png"));
            greenStar = ImageIO.read(new File("resources/GreenStar.png"));
        } catch (IOException ioe) {
            System.err.println(ioe);
            System.exit(1);
        }

        this.system = system;
    }

    /**
     * renders the component
     * @param g for graphics
     */
    @Override
    public void paint(Graphics g) {
        final Graphics2D gfx = (Graphics2D) g;

        final Dimension size = getSize();

        boolean status;
        if (current) {
            status = gfx.drawImage(blueStar, 0, 0, size.width, size.height, null); 
        } else if (inRange) {
            status = gfx.drawImage(greenStar, 0, 0, size.width, size.height, null);
        } else {
            status = gfx.drawImage(redStar, 0, 0, size.width, size.height, null);
        }
        
        if (!status) {
        	System.out.println("Not done drawing! ignoring...");
        }
    }

    /**
     * Used to set whether the player is on this star system.
     * @param current
     */
    public void setCurrent(boolean current) {
        this.current = current;
    }

    /**
     * Used to set whether the player can warp to this star system.
     * @param inRange
     */
    public void setInRange(boolean inRange) {
        this.inRange = inRange;
        this.system.setInRange(inRange);
    }

    /**
     * @return the star system associated with this view
     */
    public StarSystem getStarSystem() {
        return system;
    }
}
